package com.aaron.demo1.test.megaeyes.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import ideal4j.icity.common.enumtype.CodeEnumType;

import java.util.List;

/**
 * @ClassName:  TableUtil   
 * @Description: 列表展示工具类
 * @author: zhangbo
 * @date: 2018年9月6日 上午9:31:22
 */
public class TableUtil {

    private TableUtil(){}
    /**
     * 用于返回状态码
     * @param type
     * @return
     */
    public static String tojson(CodeEnumType type) {
        String json = "{\"code\":\""+type.code+"\",\"message\":\""+type.value+"\"}";
        return json;
    }

    /**
     * 状态码组装
     * @param code
     * @return
     */
    public static String tojson(int code) {
        String json = "{\"code\":\""+code+"\",\"message\":\""+CodeEnumType.getValue(code)+"\"}";
        return json;
    }

    /**
     * 状态码，message
     * @param code
     * @param name
     * @return
     */
    public static String tojson(int code,String name) {
        String json = "{\"code\":\""+code+"\",\"message\":\""+name+"\"}";
        return json;
    }

	
//	/**
//	 * 用于列表查询
//	 * @param pageInfo
//	 * @return
//	 */
//    @SuppressWarnings("rawtypes")
//    public static String tojson(PageInfo pageInfo) {
//        String data = JSON.toJSONString(pageInfo.getList(), SerializerFeature.WriteMapNullValue);
//        String json = "{\"code\":\""+CodeEnumType.SUCCESS.getCode()+"\",\"message\":\""+CodeEnumType.SUCCESS.getValue()+"\",\"total\":" + pageInfo.getTotal() + ",\"page\":"+pageInfo.getPageNum()+",\"pageSize\":"+pageInfo.getPageSize()+",\"data\":"
//                + data + "}";
//        return json;
//    }
    
    /**
     * 用于参数下来
     * @param list
     * @return
     */
    public static <T> String tojson(List<T> list) {
        String data = JSON.toJSONString(list, SerializerFeature.WriteMapNullValue);
        String json = "{\"code\":\""+CodeEnumType.SUCCESS.getCode()+"\",\"message\":\""+CodeEnumType.SUCCESS.getValue()+"\",\"data\":"
                + data + "}";
        return json;
    }
    
    /**
     * 用于详情查询
     * @param t
     * @return
     */
    public static <T> String tojson(T t) {
        String data = JSON.toJSONString(t, SerializerFeature.WriteMapNullValue);
        String json = "{\"code\":\""+CodeEnumType.SUCCESS.getCode()+"\",\"message\":\""+CodeEnumType.SUCCESS.getValue()+"\",\"data\":"
                + data + "}";
        return json;
    }
    
    /**
     * 获取成功返回值
     */
	public static JSONObject getSuccessResult() {
		JSONObject result = new JSONObject();
		result.put("code", CodeEnumType.SUCCESS.getCode());
		result.put("message", CodeEnumType.SUCCESS.getValue());
		return result;
	}

//    /**
//     * 用于列表查询
//     * @param pageInfo
//     * @return
//     */
//    public static String toJSONStringWithDateFormat(PageInfo pageInfo,String dateFormat) {
//        String data = JSON.toJSONStringWithDateFormat(pageInfo.getList(),dateFormat, SerializerFeature.WriteMapNullValue , SerializerFeature.DisableCircularReferenceDetect);
//        String json = "{\"code\":\""+CodeEnumType.SUCCESS.getCode()+"\",\"message\":\""+CodeEnumType.SUCCESS.getValue()+"\",\"total\":" + pageInfo.getTotal() + ",\"page\":"+pageInfo.getPageNum()+",\"pages\":"+pageInfo.getPages()+",\"pageSize\":"+pageInfo.getPageSize()+",\"data\":"
//                + data + "}";
//        return json;
//    }
    /**
     * 用于 Object
     * @param obj
     * @return
     */
    public static String toJSONStringWithDateFormat(Object obj,String dateFormat) {
        String data = JSON.toJSONStringWithDateFormat(obj,dateFormat, SerializerFeature.WriteMapNullValue , SerializerFeature.DisableCircularReferenceDetect);
        String json = "{\"code\":\""+CodeEnumType.SUCCESS.getCode()+"\",\"message\":\""+CodeEnumType.SUCCESS.getValue()+"\",\"data\":" + data + "}";
        return json;
    }

    /**
     * 用于 Object
     * @param obj
     * @return
     */
    public static String toJSONStringWithDateFormatForInfo(Object obj,String dateFormat) {
        return JSON.toJSONStringWithDateFormat(obj,dateFormat, SerializerFeature.WriteMapNullValue , SerializerFeature.DisableCircularReferenceDetect);
    }
}
